package cn.chenshiwu121.service.impl;

import cn.chenshiwu121.mapper.ContentCategoryMapper;
import cn.chenshiwu121.pojo.ContentCategory;
import cn.chenshiwu121.pojo.ContentCategoryExample;
import cn.chenshiwu121.pojo.EasyUITreeNode;
import cn.chenshiwu121.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private ContentCategoryMapper mapper;

    @Override
    public List<EasyUITreeNode> getContentCategoryList(Long parentId) {
        ContentCategoryExample example = new ContentCategoryExample();
        ContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<ContentCategory> list = mapper.selectByExample(example);
        List<EasyUITreeNode> nodes = new ArrayList<>();
        for (ContentCategory category: list) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(category.getId());
            node.setState(category.getIsParent() ? "closed" : "open");
            node.setText(category.getName());
            nodes.add(node);
        }
        return nodes;
    }

    @Override
    public ContentCategory createContentCategory(ContentCategory category) throws Exception {
        category.setIsParent(false);
        category.setCreated(new Date());
        category.setUpdated(category.getCreated());
        category.setSortOrder(1);
        category.setStatus(1);
        ContentCategory parent = mapper.selectByPrimaryKey(category.getParentId());
        if (!parent.getIsParent()) {
            parent.setIsParent(true);
            updateContentCategory(parent);
        }
        mapper.insert(category);
        return category;
    }

    @Override
    public void updateContentCategory(ContentCategory category) throws Exception {
        int rows = mapper.updateByPrimaryKeySelective(category);
        if (rows < 1) throw new Exception("ERROR:update failed");
    }

    @Override
    public void deleteContentCategory(Long id) throws Exception {
        ContentCategory category = mapper.selectByPrimaryKey(id);
        if (category.getIsParent()) {
            throw new Exception("ERROR:cannot delete category that has subclasses");
        }
        int rows = mapper.deleteByPrimaryKey(id);
        if (rows < 1) throw new Exception("ERROR:delete failed");

        // 查询当前类是否是最后一个被删除的
        ContentCategoryExample example = new ContentCategoryExample();
        ContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        long count = mapper.countByExample(example);
        if (count < 1) {
            ContentCategory parent = new ContentCategory();
            parent.setId(category.getParentId());
            parent.setIsParent(false);
            mapper.updateByPrimaryKeySelective(parent);
        }
    }
}
